package lx.exalarm.base;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import lx.exalarm.R;
import lx.exalarm.alarm.ActionBean;
import lx.exalarm.manager.GlobalThreadManager;

/**
 * Created by liuxu on 15-3-5.
 */
public abstract class BaseSimplePlayActivity extends BasePlayActivity
        implements View.OnClickListener {

    private RelativeLayout mContainer;
    private TextView mBtnStop;
    private TextView mBtnNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopAction(mAction);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initView(ActionBean action, ActionBean next) {
        setContentView(R.layout.activity_play_base_simple);
        mContainer = getView(R.id.apbs_container);
        mBtnNext = getView(R.id.apbs_btn_next);
        mBtnStop = getView(R.id.apbs_btn_stop);
        mBtnNext.setOnClickListener(this);
        mBtnStop.setOnClickListener(this);

        if (next == null) {
            // no more available action
            mBtnNext.setEnabled(false);
            mBtnNext.setVisibility(View.GONE);
        } else {
            mBtnNext.setEnabled(true);
            mBtnNext.setVisibility(View.VISIBLE);
        }

        init(action, next);
        startAction(action);
    }

    @Override
    protected void dataChanged(ActionBean action, ActionBean next) {
        if (next == null) {
            // no more available action
            mBtnNext.setEnabled(false);
            mBtnNext.setVisibility(View.GONE);
        } else {
            mBtnNext.setEnabled(true);
            mBtnNext.setVisibility(View.VISIBLE);
        }
        startAction(action);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.apbs_btn_stop: {
                stopAction(mAction);
                finish();
                break;
            }
            case R.id.apbs_btn_next: {
                stopAction(mAction);
                startNextAction();
                finish();
                break;
            }
        }
    }

    protected RelativeLayout getViewContainer() {
        return mContainer;
    }

    protected void startAnim() {
        final View alarmView = getView(R.id.apbs_anim_alarm);
        final View animView1 = getView(R.id.apbs_anim_view1);
        final View animView2 = getView(R.id.apbs_anim_view2);
        final View animViewContainer = getView(R.id.apbs_anim_view_container);
        final Animation alarmAnim = generateAlarmAnim();
        final Animation anim1 = generateCircleAnim();
        final Animation anim2 = generateCircleAnim();

        animViewContainer.setVisibility(View.VISIBLE);
        animViewContainer.bringToFront();
        animView1.setAnimation(anim1);
        alarmView.setAnimation(alarmAnim);

        anim1.start();
        alarmAnim.start();
        GlobalThreadManager.runInUiThreadDelayed(new Runnable() {
            @Override
            public void run() {
                animView2.setAnimation(anim2);
                anim2.start();
            }
        }, 400);
    }

    protected void stopAnim() {
        final View alarmView = getView(R.id.apbs_anim_alarm);
        final View animView1 = getView(R.id.apbs_anim_view1);
        final View animView2 = getView(R.id.apbs_anim_view2);
        final View animViewContainer = getView(R.id.apbs_anim_view_container);
        alarmView.clearAnimation();
        animView1.clearAnimation();
        animView2.clearAnimation();
        animViewContainer.setVisibility(View.GONE);
    }

    private Animation generateCircleAnim() {
        ScaleAnimation anim = new ScaleAnimation(
                1.0f, 8.0f, 1.0f, 8.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(1200);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setRepeatMode(Animation.RESTART);
        anim.setInterpolator(new AccelerateInterpolator());
        return anim;
    }

    private Animation generateAlarmAnim() {
        RotateAnimation anim = new RotateAnimation(-3.0f, 3.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(150);

        anim.setRepeatCount(Animation.INFINITE);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        return anim;
    }

    protected abstract void init(ActionBean action, ActionBean next);

    protected abstract void startAction(ActionBean action);

    protected abstract void stopAction(ActionBean action);

}
